<?php
namespace Admin\Controller;
use Lib\Api\CustomerApi;
use Lib\Api\GameTypeApi;
use Lib\Api\OrganizationApi;
use Lib\Api\OrganizationCommissionSetApi;
use Lib\Api\OrganizationRoleApi;
use Lib\Api\RoleApi;
use Think\Controller;
use Think\Model;
class CustomerController extends Controller {
    private $modelName = 'Customer';

    public function index(){
        $this->display();
    }

    public function operation_center() {
        $this->assign('level', 1);
        $this->display("index");
    }

    public function member_unit() {
        $this->assign('level', 2);
        $this->display("index");
    }

    public function micro_ring() {
        $this->assign('level', 3);
        $this->display("index");
    }

    public function tolist() {
        /*$page_size = $_POST['page_size'];
        $page_num = $_POST['page_num'];
        $keyword = $_POST['keyword'];

        $map = array();
        if ($keyword <> "")
            $map['name'] = array('like', '%'.$keyword.'%');
        */

        $page_size = $_POST['page_size'];
        $page_num = $_POST['page_num'];
        $organization_id = session('admin_id');

        $level = $_POST['level'];

        $AGENTCONFIG = C('AGENTCONFIG');
        // $level = 0;
        $field = "a.id, a.name, a.real_name, a.mobile_phone, b.balance, a.state, a.audit_state, ifnull(a.last_login_ip, '--') as last_login_ip, ifnull(a.last_login_time, '--') as last_login_time, a.create_time, b.id as organization_id, b.name as organization_name, b.code, b.p_code, b.level, b.risk_control_rate, e.name as role_name";

        $where["a.audit_state"] = array("neq", 3);
        $where["b.level"] = $level;
        $where["c.belong_organization_id"] = $organization_id;
        $where['DATE_FORMAT(c.datetime, "%Y-%m-%d %H:%i:%s")'] = array('elt', 'DATE_FORMAT("'.date('Y-m-d H:i:s').'", "%Y-%m-%d %H:%i:%s")');
        $left = 'left join fs_organization b on b.id = a.organization_id';
        $left .= ' left join fs_organization_belong c on c.organization_id = b.id';
        $left .= ' left join fs_organization_role d on d.organization_id = a.organization_id';
        $left .= ' left join fs_role e on e.id = d.role_id';

        /*$left = 'left join fs_user_belong b on b.user_id = a.id';
        if ($user_id > 0) {
            $where["b.belong_user_id"] = $user_id;
        }

        if ($level > 0) {
            for($i=0; $i<$level; $i++) {
                $field .= ',ifnull(u'.$i.'.real_name, "--") as p_'.$i.'name';
                $left .= ' left join fs_user_belong ub'.$i.' on ub'.$i.'.user_id = a.id and ub'.$i.'.belong_level = '.$i;
                $left .= ' left join fs_user u'.$i.' on u'.$i.'.id = ub'.$i.'.belong_user_id';
            }
        }*/

        $list = CustomerApi::getList($where, $left, $page_num, $page_size, $field, 'a.create_time desc');
        $total = CustomerApi::getTotal($where, $left);
        $user_list = pageHelper($page_size, $page_num, $list, $total);
        $user_list['setting'] = $AGENTCONFIG[$level]['setting'];

        $user_list['type'] = $AGENTCONFIG[$level]['type'];
        $rel['data'] = $user_list;
        $rel['config'] = $AGENTCONFIG[session('admin_level')]['config'];
        $rel['sum_balance'] = CustomerApi::getSum($where, "b.balance", $left);
        $this->ajaxReturn($rel);

    }

    // 更改客户状态
    public function changeState() {
        $model = M($this->modelName);
        $this->ajaxReturn(CustomerApi::save($model->create($_POST, Model::MODEL_UPDATE)));
    }

    // 获取用户信息
    public function getCustomerInfo() {
        $id = $_POST['id'];
        $customer = CustomerApi::getInfo(array("a.id"=>$id), "a.id, a.name, a.real_name, a.mobile_phone, b.id as organization_id, b.name as organization_name, b.level");
        $AGENTCONFIG = C('AGENTCONFIG');
        $customer['setting'] = $AGENTCONFIG[$customer['level']]['setting'];
        $this->ajaxReturn($customer);
    }

    // 修改或添加帐户信息
    public function addoreditdata() {
        //$data['id'] = $_POST['id'];
        $modelObj = M($this->modelName);
        $data = $modelObj->create($_POST);
        //数据校验
        if (empty($data['organization_id'])) {
            $ajaxdata['code'] = -1;
            $ajaxdata['msg'] = '未选择所属机构';
            $this->ajaxReturn($ajaxdata);
        }
        if (empty($data['name'])) {
            $ajaxdata['code'] = -1;
            $ajaxdata['msg'] = '未填写登录帐号';
            $this->ajaxReturn($ajaxdata);
        }
        if (mb_strlen($data['name']) < 6) {
            $ajaxdata['code'] = -1;
            $ajaxdata['msg'] = '请输入长度6位或以上的登录账号';
            $this->ajaxReturn($ajaxdata);
        }
        if (empty($_POST['id'])) {
            if (empty($data['password'])) {
                $ajaxdata['code'] = -1;
                $ajaxdata['msg'] = '未填写登录密码';
                $this->ajaxReturn($ajaxdata);
            }
            if (strlen($data['password']) < 6) {
                $ajaxdata['code'] = -1;
                $ajaxdata['msg'] = '请输入长度6位或以上的登录密码';
                $this->ajaxReturn($ajaxdata);
            }
        }
        if (empty($data['real_name'])) {
            $ajaxdata['code'] = -1;
            $ajaxdata['msg'] = '未填写客户姓名';
            $this->ajaxReturn($ajaxdata);
        }
        if (empty($data['mobile_phone'])) {
            $ajaxdata['code'] = -1;
            $ajaxdata['msg'] = '未填写用户手机';
            $this->ajaxReturn($ajaxdata);
        } else if (!preg_match("/^[0-9][0-9]{1}\\d{9}$/", $data['mobile_phone'])) {
            $data['code'] = -1;
            $data['msg'] = '手机号码不正确';
            $this->ajaxReturn($data);
            exit;
        }
        // $roles = $_POST['roles'];
        $ajaxdata['code'] = 0;
        if (empty($_POST['id'])) {
            //添加账号是否存在
            $result = CustomerApi::getTotal(array('name' => $data['name']), "");
            if ($result == 0) {
                //账号不存在，添加管理员
                $data['password'] = md5($data['password']);
                $data['state'] = 1;
                $data['audit_state'] = 1;
                $data['create_time'] = date('Y-m-d H:i:s');
                $id = CustomerApi::add($data);
                $ajaxdata['msg'] = '添加成功';
            } else {
                $ajaxdata['code'] = -1;
                $ajaxdata['msg'] = '添加失败，客户账号已存在';
            }
        } else {

            $where['_string'] = 'name="'.$data['name'].'" and id not in('.$data['id'].') ';
            $result = CustomerApi::getTotal($where, "");
            if ($result == 0) {
                $data['update_time'] = date('Y-m-d H:i:s');
                CustomerApi::save($data);
                $ajaxdata['customer'] = $data;
                $ajaxdata['msg'] = '修改成功';
            } else {
                $ajaxdata['code'] = -1;
                $ajaxdata['msg'] = '修改失败，客户账号已存在';
            }
        }
        $this->ajaxReturn($ajaxdata);
    }

    // 重置密码
    public function resetPass() {
        $data['password'] = md5("123456");
        $map["id"] = $_POST['id'];
        CustomerApi::saveByCondition($map, $data);
        $ajaxdata['code'] = 1;
        $ajaxdata['msg'] = '重置密码成功，重置后密码为：123456';
        $this->ajaxReturn($ajaxdata);
    }
}